package team.cloud.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceList;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.springframework.beans.factory.annotation.Autowired;
import team.cloud.entity.vo.ResultVO;
import team.cloud.k8s.Fabric8;
import team.cloud.k8s.K8sServices;
import team.cloud.util.ResultVOUtil;

import javax.validation.Valid;
import java.util.List;

@org.springframework.stereotype.Service
public class K8sServiceService {
    @Autowired
    private Fabric8 fabric8;

    @Autowired
    private K8sServices k8sServices;

    public ResultVO create(@Valid Service service) {
        KubernetesClient client = fabric8.connect();
        Boolean result;
        try {
            result = k8sServices.createService(client, service);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVOUtil().Fail("创建Service失败，请检查参数");
        }
        ResultVO resultVO = new ResultVOUtil().success();
        resultVO.setData(result);
        return resultVO;
    }

    public ResultVO deleteServiceOneByNamespace(String namespaceName, String service) {
        KubernetesClient client = fabric8.connect();
        Boolean result;
        try {
            result = k8sServices.deleteService(client, namespaceName, service);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVOUtil().resourceNotFound();
        }
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(0);
        resultVO.setMsg("成功");
        resultVO.setData(result);
        return resultVO;
    }

    public ResultVO deleteListByNamespace(String namespaceName) {
        KubernetesClient client = fabric8.connect();
        Boolean result;
        try {
            result = k8sServices.deleteServices(client, namespaceName);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVOUtil().resourceNotFound();
        }
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(0);
        resultVO.setMsg("成功");
        resultVO.setData(result);
        return resultVO;
    }

    public ResultVO deleteAll() {
        KubernetesClient client = fabric8.connect();
        Boolean result;
        try {
            result = k8sServices.deleteAllServices(client);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVOUtil().Fail("删除失败，请联系管理员");
        }
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(0);
        resultVO.setMsg("成功");
        resultVO.setData(result);
        return resultVO;
    }

    public ResultVO getOneByNamespace(String namespaceName, String service) {
        KubernetesClient client = fabric8.connect();
        Service result;
        try {
            result = k8sServices.getService(client, namespaceName, service);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVOUtil().resourceNotFound();
        }
        JSONObject list = setService(result);
        return new ResultVOUtil().success(list.toString());
    }

    public ResultVO getByNamespace(String namespaceName) {
        KubernetesClient client = fabric8.connect();
        ServiceList result;
        try {
            result = k8sServices.listServicesFromNamespace(client, namespaceName);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVOUtil().resourceNotFound();
        }
        List<Service> list = result.getItems();
        JSONArray resultAll = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            JSONObject list1 = setService(list.get(i));
            resultAll.add(list1);
        }
        return new ResultVOUtil().success(resultAll.toString());
    }

    public ResultVO getList() {
        KubernetesClient client = fabric8.connect();
        ServiceList result;
        try {
            result = k8sServices.listServices(client);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVOUtil().Fail("查找失败，请联系管理员");
        }
        List<Service> list = result.getItems();
        JSONArray resultAll = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            JSONObject list1 = setService(list.get(i));
            resultAll.add(list1);
        }
        return new ResultVOUtil().success(resultAll.toString());
    }

    private JSONObject setService(Service result) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ApiVersion", result.getApiVersion());
        jsonObject.put("Kind", result.getKind());
        if (result.getMetadata() != null) {
            jsonObject.put("Name", result.getMetadata().getName());
            jsonObject.put("Namespace", result.getMetadata().getNamespace());
        } else {
            jsonObject.put("Name", "");
            jsonObject.put("Namespace", "");
        }
        if (result.getMetadata().getLabels() != null) {
            jsonObject.put("Labels", result.getMetadata().getLabels().toString());
        } else {
            jsonObject.put("Labels", "");
        }
        if (result.getSpec().getPorts().get(0).getPort() != null) {
            jsonObject.put("Port", result.getSpec().getPorts().get(0).getPort().toString());
        } else {
            jsonObject.put("Port", "");
        }
        if (result.getSpec().getPorts().get(0).getTargetPort() != null) {
            jsonObject.put("TargetPort", result.getSpec().getPorts().get(0).getTargetPort().toString());
        } else {
            jsonObject.put("TargetPort", "");
        }
        if (result.getSpec().getPorts().get(0) != null) {
            jsonObject.put("Protocol", result.getSpec().getPorts().get(0).getProtocol());
        } else {
            jsonObject.put("Protocol", "");
        }
        if (result.getSpec().getSelector() != null) {
            jsonObject.put("Selector", result.getSpec().getSelector().toString());
        } else {
            jsonObject.put("Selector", "");
        }
        return jsonObject;
    }
}

